/********************************************************************
	created:	2011/05/16
	created:	16:5:2011   18:56
	author:		wuxiangfei
*********************************************************************/

#include "stdafx.h"
#include "TcpServe.h"
#include "SocMesWnd.h"

#include <algorithm>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNAMIC(CSocMesWnd, CWnd)

CSocMesWnd::CSocMesWnd()
{
	m_pTcpServe = NULL;
}

CSocMesWnd::~CSocMesWnd()
{
}

BEGIN_MESSAGE_MAP(CSocMesWnd, CWnd)
	ON_MESSAGE(SOC_NETSERVE, OnTCPServe)
END_MESSAGE_MAP()

LRESULT CSocMesWnd::OnTCPServe( WPARAM wParam, LPARAM lParam )
{
	int nIndex;
	int nSendID;
	int nAddLen = sizeof( sockaddr_in );
	sockaddr_in uSocAddIn;
	SOCKET socAccept;
	SERVE_LPARAM uData;
	int nReceiveLen;

	uData.m_pData = m_pTcpServe->m_pData;

	if( m_pTcpServe->m_pbOpenSev && wParam == m_pTcpServe->m_socServer)
	{
		switch( lParam )
		{
		case FD_ACCEPT:
			socAccept = accept( m_pTcpServe->m_socServer, (sockaddr*)&uSocAddIn, &nAddLen );
			if( socAccept != INVALID_SOCKET )
			{
				nSendID = m_pTcpServe->m_nNextID;
				m_pTcpServe->m_nNextID++;
				m_pTcpServe->m_nNextID = (m_pTcpServe->m_nNextID==INT_MAX) ? 1 : m_pTcpServe->m_nNextID;
				m_pTcpServe->m_pbSocOpen.push_back( true );
				m_pTcpServe->m_pnSocID.push_back( nSendID );
				m_pTcpServe->m_pSocData.push_back( socAccept );

				uData.m_nSucSoc = nSendID;
				
				uData.m_nPosOfSunSoc = 0;

				(*m_pTcpServe->m_pFuncMes)(NET_TCPSERVERACCEPT, (LPARAM)&uData );
			}
			/*else
			{
				uData.m_nSucSoc = -1;
				if (m_pTcpServe->m_pFuncMes != NULL)
				{
					(*m_pTcpServe->m_pFuncMes)(NET_TCPSERVERACCEPT, (LPARAM)&uData );
				}
			}*/
			break;
		
		case FD_CLOSE:
		case FD_READ:
		case FD_WRITE:
		case FD_OOB:
		default:
			break;
		}


		return NULL;
	}

	vector<SOCKET>::iterator iteFind;
	iteFind = find( m_pTcpServe->m_pSocData.begin(), m_pTcpServe->m_pSocData.end(), (SOCKET)wParam );
	
	if( iteFind == m_pTcpServe->m_pSocData.end() )
	{
		return NULL;
	}
	else
	{
		nIndex = iteFind - m_pTcpServe->m_pSocData.begin();
		nSendID = m_pTcpServe->m_pnSocID[nIndex];
	}

	if( m_pTcpServe->m_pbSocOpen[nIndex] )
	{
		switch( lParam )
		{
		case FD_CLOSE:
			closesocket( m_pTcpServe->m_pSocData[nIndex] );
			m_pTcpServe->m_pbSocOpen.erase( m_pTcpServe->m_pbSocOpen.begin() + nIndex );
			m_pTcpServe->m_pnSocID.erase( m_pTcpServe->m_pnSocID.begin() + nIndex );
			m_pTcpServe->m_pSocData.erase( m_pTcpServe->m_pSocData.begin() + nIndex );

			uData.m_nSucSoc = nSendID;
			uData.m_nPosOfSunSoc = nIndex;

			(*m_pTcpServe->m_pFuncMes)(NET_TCPSERVERCLOSE, (LPARAM)&uData);

			break;

		case FD_READ:
			nReceiveLen = recv( m_pTcpServe->m_pSocData[nIndex], (char*)m_pTcpServe->m_pucDataBuf, m_pTcpServe->m_nBufLen, 0 );
			m_pTcpServe->m_nDataLen = nReceiveLen < 0 ? 0 : nReceiveLen;
			uData.m_nSucSoc = nSendID;
			uData.m_nPosOfSunSoc = nIndex;
			(*m_pTcpServe->m_pFuncMes)(NET_TCPSERVERRECEIVE, (LPARAM)&uData);

			break;

		case FD_WRITE:
			uData.m_nSucSoc = nSendID;
			uData.m_nPosOfSunSoc = nIndex;
			(*m_pTcpServe->m_pFuncMes)(NET_TCPSERVERSEND, (LPARAM)&uData);
			
			break;

		case FD_OOB:
		default:
			break;
		}
	}

	return NULL;
}